/**
 * FileName: popupEditSimpleView.js Summary: 单表基类。 Encoding: UTF-8 Author:
 * 方思扬(fangsiyang@hotmail.com) Version: 1.0 Date: 2008-5-19
 */
Ext.namespace("Gfa.view");
Gfa.view.PopupEditSimpleView = function() {
	this.doLayout();
};

Gfa.view.PopupEditSimpleView.prototype = {

	/**
	 * @cfg {int} pageSize 页面大小，默认为15。
	 */
	pageSize : 15,

	/**
	 * @cfg {function} service DWR类
	 */
	service : null,

	/**
	 * @cfg {String} className 类名（包名+类名）
	 */
	className : null,

	/**
	 * @cfg {String} methodName 方法名
	 */
	methodName : null,

	/**
	 * @cfg {Ext.grid.ColumnModel} cm 列模式。
	 */
	cm : null,

	/**
	 * @cfg {Ext.data.QueryReader} reader 如果没有设置，默认根据cm进行生成。
	 */
	reader : null,

	popupWidth : 640,

	popupHeight : 480,

	popupTitle : '',

	operation : new Gfa.core.Operation(),

	/**
	 * 本方法须在子类中进行覆盖，以初始化cm以及store。
	 */
	init : function() {

	},

	createStore : function() {
		if (!this.reader) {
			var fields = [];

			for (var i = 0; i < this.cm.getColumnCount(); i++) {
				fields.push(this.cm.getDataIndex(i));
			}

			this.reader = new Gfa.data.DwrReader({}, fields);
		}

		var store = new Gfa.data.Store({
			proxy : new Gfa.data.DwrProxy(this.service),
			reader : this.reader
		});

		store.on('beforeLoad', function(store, options) {
			if (options.params.start === undefined) {
				options.params.start = 0;
			}

			var queryInfo = Gfa.QueryInfoHelper.buildQueryInfo(options.params,
					this.pnlQuery, {
						queryObjectName : this.className
					});

			store.proxy.DWR = CommonService;
			options.params.dwrMethod = "findByQueryInit";
			options.params.dwrArguments = [queryInfo, []];
		}, this);

		store.on('beforeSave', function(store, params) {
			store.proxy.DWR = this.service;
			params.dwrMethod = this.methodName;
		}, this);

		return store;
	},

	initQueryPanel : function() {
		this.pnlQuery = new Ext.form.FormPanel({
			bodyStyle : 'padding:5px 3px 0;',
			labelAlign : 'left',
			closable : true,
			border : false,
			bbar : this.toolbar,
			items : [this.createQueryComposite()]
		});
	},

	createToolbar : function() {
		return new Ext.Toolbar({
			items : [
					'-',
					Gfa.ToolbarHelper.getNewItem(this.operation.add,
							this.operation),
					Gfa.ToolbarHelper.getEditItem(this.operation.edit,
							this.operation),
					Gfa.ToolbarHelper.getDeleteItem(this.operation.del,
							this.operation),
					Gfa.ToolbarHelper.getSaveItem(this.operation.save,
							this.operation),
					Gfa.ToolbarHelper.getSearchItem(this.operation.search,
							this.operation),
					'-',
					Gfa.ToolbarHelper.getResetItem(this.operation.reset,
							this.operation)]
		});
	},

	createGrid : function() {
		return new Ext.grid.EditorGridPanel({
			store : this.store,
			cm : this.cm,
			tbar : this.pnlQuery,
			bbar : new Ext.PagingToolbar({
				store : this.store,
				displayInfo : true,
				pageSize : this.pageSize
			}),
			sm : new Ext.grid.RowSelectionModel({
				singleSelect : false
			}),
			border : false,
			autoScroll : true,
			loadMask : true,
			anchor : '0 0'
		});
	},

	createPopWindow : function() {
		this.frmPopup = this.createEditComposite();

		return new Ext.Window({
			layout : 'fit',
			width : this.popupWidth,
			height : this.popupHeight,
			closeAction : 'hide',
			plain : true,
			modal : true,
			title : this.popupTitle,
			items : [this.frmPopup],
			buttons : [{
				text : i18n.global.save,
				handler : this.saveAction,
				scope : this
			}, {
				text : i18n.global.close,
				handler : this.cancelAction,
				scope : this
			}]
		});
	},

	/**
	 * 弹出窗口的保存动作。
	 */
	saveAction : function() {
		// 如果客户端校验失败，直接返回。
		if (!this.frmPopup.form.isValid())
			return;

		if (this.operation.eidtMode == 'new') {
			var record = this.store.insertNewRecord();
			this.frmPopup.form.updateRecord(record);
		} else {
			var record = this.operation.getSelected();
			this.frmPopup.form.updateRecord(record);
		}

		// 执行指定操作
		// TODO

		this.winPopup.hide();
	},

	/**
	 * 弹出窗口的取消动作。
	 */
	cancelAction : function() {
		this.winPopup.hide();
	},

	/**
	 * 创建查询区域。
	 */
	createQueryComposite : function() {

	},

	/**
	 * 创建编辑区域，这里是以弹出窗口的方式。
	 */
	createEditComposite : function() {

	},

	/**
	 * 初始化弹出窗口，当弹出窗口Show出来后，为某些字段赋值。
	 */
	initPopupData : function() {
		this.onPopupShow();
	},

	doLayout : function() {
		this.init();
		this.toolbar = this.createToolbar();
		this.initQueryPanel();
		this.store = this.createStore();
		this.pnlGrid = this.createGrid();
		this.winPopup = this.createPopWindow();

		this.pnlGrid.on('rowdblclick', function(grid, rowIdx, e) {
			this.operation.edit();
		}, this);

		// 如果子类中实现了onPopupShow方法，则注册弹出窗口的show事件。
		if (this.onPopupShow) {
			this.winPopup.on('show', this.initPopupData, this);
		}

		this.operation.queryComposite = this.pnlQuery;
		this.operation.pnlGrid = this.pnlGrid;
		this.operation.store = this.store;
		this.operation.popWindow = this.winPopup;
		this.operation.scope = this;

		var renderTo = Ext.getCmp(Gfa.GlobalVariable.contentAreaId).activeTab;
		renderTo.add(this.pnlGrid);
		renderTo.doLayout();

		this.store.load({
			params : {
				start : 0,
				limit : this.pageSize
			}
		});
	}
};